package com.sharemarking.application.common.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.sharemarking.application.common.dto.DictDto;
import com.sharemarking.application.common.entity.Dict;
import com.sharemarking.application.common.repository.DictRepo;

@Service
public class DictService {
	@Autowired
	private DictRepo dr;
	
	@Cacheable("dict")
	public List<DictDto> getDict(String type){
		List<Dict> list=dr.findAllByTypeOrderBySort(type);
		List<DictDto> target=new ArrayList<DictDto>();
		for(Dict dict:list){
			DictDto dd=new DictDto();
			dd.setLabel(dict.getLabel());
			dd.setValue(dict.getValue());
			target.add(dd);
		}
		return target;
	}
	
	
	public Iterable<Dict> getAllDicts(){
		return dr.findAll(new Specification<Dict>() {
			
			@Override
			public Predicate toPredicate(Root<Dict> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
				return cb.notEqual(root.get("type").as(String.class), "ADMINISTRATOR");
			}
		});
	}
	
	
	
}
